# 银行管理系统/main.py
from models import Bank
from tools import input_amount

class App:
    def __init__(self):
        self.bank = Bank()

    def run(self):
        self._welcome()
        if not self._login():
            print("多次错误，系统退出")
            return

        while True:
            self._menu()
            ch = input(">>> ").strip().upper()
            if ch == "1":
                self._create()
            elif ch == "2":
                self._query()
            elif ch == "3":
                self._withdraw()
            elif ch == "4":
                self._deposit()
            elif ch == "5":
                self._transfer()
            elif ch == "6":
                self._lock()
            elif ch == "7":
                self._unlock()
            elif ch == "Q":
                if self.bank.check_admin(*self._input_admin()):
                    print("系统已退出")
                    break
                else:
                    print("验证失败，返回菜单")
            else:
                print("输入无效")

    # ---------- 界面 ----------
    def _welcome(self):
        print("******************************************")
        print("***        欢迎登录银行管理系统         ***")
        print("******************************************")

    def _menu(self):
        print("******************************************")
        print("***   1.开户(1)            2.查询(2)    ***")
        print("***   3.取款(3)            4.存款(4)    ***")
        print("***   5.转账(5)            6.锁定(6)    ***")
        print("***   7.解锁(7)                        ***")
        print("***                                   ***")
        print("***    退出(Q)                         ***")
        print("******************************************")

    def _input_admin(self):
        user = input("管理员账户：")
        pwd = input("管理员密码：")
        return user, pwd

    def _login(self):
        for _ in range(3):
            if self.bank.check_admin(*self._input_admin()):
                print("登录成功，请稍后...")
                return True
            print("管理员账户或密码输入错误...")
        return False



    def _create(self):
        name = input("姓名：")
        id_card = input("身份证号：")
        phone = input("手机号：")
        money = input_amount("预存金额：")
        if money < 0:
            print("金额非法")
            return
        pwd = input("密码：")
        cid = self.bank.create_user(name, id_card, phone, money, pwd)
        print(f"开户成功，卡号：{cid}")

    def _query(self):
        cid = input("卡号：")
        pwd = input("密码：")
        bal = self.bank.query_balance(cid, pwd)
        print(f"余额：{bal}" if bal is not None else "查询失败")

    def _withdraw(self):
        cid = input("卡号：")
        pwd = input("密码：")
        amt = input_amount("取款金额：")
        ok = self.bank.withdraw(cid, pwd, amt)
        print("取款成功" if ok else "取款失败")

    def _deposit(self):
        cid = input("卡号：")
        pwd = input("密码：")
        amt = input_amount("存款金额：")
        ok = self.bank.deposit(cid, pwd, amt)
        print("存款成功" if ok else "存款失败")

    def _transfer(self):
        out_cid = input("转出卡号：")
        in_cid = input("转入卡号：")
        amt = input_amount("转账金额：")
        pwd = input("转出卡密码：")
        ok = self.bank.transfer(out_cid, pwd, in_cid, amt)
        print("转账成功" if ok else "转账失败")

    def _lock(self):
        cid = input("卡号：")
        pwd = input("密码：")
        ok = self.bank.lock_card(cid, pwd)
        print("锁定成功" if ok else "锁定失败")

    def _unlock(self):
        cid = input("卡号：")
        pwd = input("密码：")
        ok = self.bank.unlock_card(cid, pwd)
        print("解锁成功" if ok else "解锁失败")


if __name__ == "__main__":
    App().run()